<?php

 namespace app\index\service;

 use think\Db;

 /**
  * @author xjy
  */
class PurchaseplanfollowService
 {

     //国家仓在途明细列表
     static public function getList($param)
     {
         if(empty($param['down'])){
              $titles=[];
         }else{
            $titles = [
            'purchase_plan_sn'=>'采购计划单号','shop_sale_plan_sn'=>'店铺销售计划单号','sku'=>'SKU','sku_name'=>'中文名称','need_quantity'=>'理论需求量',
            'quantity'=>'实际计划量','pr'=>'PR单号','po'=>'PO单号', 'po_quantity'=>'po数量','purchaseorder_date'=>'po生成时间','warehouse'=>'仓库',
            'warequantity'=>'入库数量','waredate'=>'入库时间','remind_quantity'=>'结余数量','out_quantity'=>'出库数量'];
         }
        return \app\index\model\PurchaseplanfollowModel::getList($param,$titles,"app\index\service\PurchaseplanfollowService::convertData");
     }
     public static function convertData($data='',$param=''){
      //   var_dump($data);exit;
         $purchase=\app\index\model\PurchaseplanfollowModel::getPercentage($param);
         
         if($data){
             $purchaseArr=array_unique(array_column($data,'po'));
             $resultWare=\app\common\service\WarehouseService::getWarehouse($purchaseArr,'*','po_sku');
             foreach($data as $key=>&$value){
                $po_sku=$value['po']."_".$value['sku'];
                $wareinfo=isset($resultWare[$po_sku])? $resultWare[$po_sku]:[];
                
                $warehouseTotal=!empty($wareinfo) ? array_sum(array_column($wareinfo,'warehouse_quantity')) : 0;
                $percentage=isset($purchase[$po_sku]['percentage']) ? $purchase[$po_sku]['percentage'] : 0;
                
                $value['purchaseorder_date']=isset($purchase[$po_sku]['purchaseorder_date']) ? $purchase[$po_sku]['purchaseorder_date']:'';
                $value['po_quantity']=!empty($percentage) ? bcmul($percentage,intval($value['quantity'])) : 0;   
                $value['warehouse']='国内7号仓';   
                $value['warequantity']=!empty($percentage) ? bcmul($percentage,intval($warehouseTotal)) : 0;
                $out_quantity=self::getDeliveryMess($value['sku'],$value['warequantity'],true);
                $value['waredate']=isset($wareinfo[0]['warehouse_date']) ? $wareinfo[0]['warehouse_date']:''; 
                $value['out_quantity']=$out_quantity; 
                $value['remind_quantity']=bcsub($value['warequantity'],$value['out_quantity']); 
             }
         }
         return $data;
     }
     /**
      * 获取发运信息
      */
     public static function getDeliveryMess($sku=[],$wareQuantity=25,$showTotal=false){    
         $condition=[];$result=[];
         $localno=\app\common\model\ToolModel::select('ship_plan_logistics',[],'local_order_no','','code');
         $total=0;$sendIds=[];
         $data=Db::name('ship_plan_send')->where('sku','=',$sku)->order('date','asc')->column('quantity','id');
         if($data && $wareQuantity>0){
             foreach($data as $key=>$value){
                 if($total<$wareQuantity){
                     $sendIds[]=$key;
                     $total+=$value;
                 }
            }   
         }
         if($showTotal){
             return $total;
         }
         if($sendIds){
            $condition['s.id']=['in',$sendIds]; 
         }else{
             return ['code'=>0,'data'=>[],'count'=>0,'msg'=>'发运信息不存在'];
         }
         $db=Db::table("ship_plan_send")->alias('s')->join('ship_plan p' ,'p.code = s.code','left')->join('ship_plan_logistics l' ,'s.code = l.code','left')  
                ->where($condition)->order('s.date asc');
        $totalDb= clone $db;
        $list=$db->field('s.*,p.user,l.local_order_no')->select();
        $count=$totalDb->count();
        return ['code'=>1,'data'=>$list,'count'=>$count];
     }

 }
